package demo.game.chess.client.ui;

import com.google.gwt.user.client.ui.DockPanel;

import demo.game.chess.common.EnumChessColor;
import demo.messaging.client.core.ClientManager;
import demo.messaging.client.core.MessageEvent;
import demo.messaging.client.core.MessageEvent.EnumMessageEventType;
import demo.messaging.client.ui.AbstractPluginUI;
import demo.messaging.client.ui.IMessagingPlugin;

public class ChessGamePlugin implements IMessagingPlugin {

	class ChessMainUI extends AbstractPluginUI {

		OnlineChessUI chessUI;

		public ChessMainUI(final MessageEvent event,
				final ClientManager clientManager) {
			super(event, clientManager);

			// the invited player takes the black side.
			createChessUI((event.getType() == EnumMessageEventType.acceptanceReceived) ? EnumChessColor.black
					: EnumChessColor.white);

			String partner = ClientManager.getClientManager()
					.getCurrentConnectedUser();
			setText("Playing chess with " + partner);

			getContentPanel().add(getChatPanel(), DockPanel.EAST);
			getContentPanel().add(this.chessUI, DockPanel.CENTER);
		}

		private void createChessUI(final EnumChessColor playerColor) {

			this.chessUI = new OnlineChessUI(playerColor);
			ClientManager.getClientManager().getEventListeners().add(
					this.chessUI);
		}

		@Override
		public void deactivate(final ClientManager clientManager) {

			clientManager.getEventListeners().remove(this.chessUI);

		}

	}

	protected AbstractPluginUI mainUI;

	public AbstractPluginUI createUI(final MessageEvent event,
			final ClientManager clientManager) {

		return new ChessMainUI(event, clientManager);
	}

	@Override
	public String getLabel() {

		return "Chess";
	}
}
